package org.xlp.db.utils;

import org.xlp.assertion.AssertUtils;
import org.xlp.db.exception.EntityException;
import org.xlp.db.sql.*;
import org.xlp.db.tableoption.xlpenum.DBType;

/**
 * Description: 分页查询对象获取工具类
 * <br/>date: 2024/1/16 11:46
 *
 * @version 1.0
 * @author: xlp
 */
public class PagedQuerySQLHelper {
    /**
     * 根据实体类型获取分页查询对象
     * @param entityClass 实体类型
     * @param <T>
     * @return
     * @throws NullPointerException 假如参数为null，则抛出该异常
     * @throws EntityException
     */
    public static <T> AbstractPagedSQL<T> getQueryPagedSQL(Class<T> entityClass){
        return getQueryPagedSQL(entityClass, true);
    }

    /**
     * 获取联合查询SQL对象
     * @param query
     * @return
     * @throws NullPointerException 假如参数为null，则抛出该异常
     * @throws EntityException
     */
    public static UnionSQL getUnionSQL(Query query){
        AssertUtils.isNotNull(query, "query parameter must be not null!");
        if (DBTypeUtils.getDbType() != DBType.ORACLE_DB){
            return UnionSQL.of(query);
        }
        return OracleUnionSQL.of(query);
    }

    /**
     * 获取多实体连接查询SQL对象
     *
     * @param beanClass
     * @param alias 实体类别名
     * @return
     * @throws NullPointerException 假如第一个参数为null，则抛出该异常
     * @throws EntityException
     */
    public static <T> ComplexQuerySQL getComplexQuerySQL(Class<T> beanClass, String alias){
        AssertUtils.isNotNull(beanClass, "beanClass parameter must be not null!");
        if (DBTypeUtils.getDbType() != DBType.ORACLE_DB){
            return ComplexQuerySQL.of(beanClass, alias);
        }
        return OracleComplexQuerySQL.of(beanClass, alias);
    }

    /**
     * 获取多实体连接查询SQL对象
     *
     * @param beanClass
     * @return
     * @throws NullPointerException 假如第一个参数为null，则抛出该异常
     * @throws EntityException
     */
    public static <T> ComplexQuerySQL getComplexQuerySQL(Class<T> beanClass){
        return getComplexQuerySQL(beanClass, null);
    }

    /**
     * 根据实体类型获取分页查询对象
     * @param entityClass 实体类型
     * @param  autoCreateCountSql 标记是否自动创建countSql，true: 自动创建， false：不创建
     * @return
     * @throws NullPointerException 假如参数为null，则抛出该异常
     * @throws EntityException
     */
    public static <T> AbstractPagedSQL<T> getQueryPagedSQL(Class<T> entityClass, boolean autoCreateCountSql){
        AssertUtils.isNotNull(entityClass, "entityClass parameter must be not null!");
        AbstractPagedSQL<T> abstractPagedSQL;
        if (DBTypeUtils.getDbType() != DBType.ORACLE_DB){
            abstractPagedSQL = new MYSQLLimitSQL<>(entityClass);
        } else {
            abstractPagedSQL = new OraclePageSQL<>(entityClass);
        }
        abstractPagedSQL.setAutoCreateCountSql(autoCreateCountSql);
        return abstractPagedSQL;
    }
}
